// pages/workerCode/home.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title:'加载中',
    filter:[],
    working:0,
    dropShow:false,
    top: 0, //标题图标距离顶部距离
    opacity: 1,
    statusBarHeight:20,
    scrollTop: 0.5,
    loading:false,
    latitude:0,
    longitude:0,
    hasFilter:0,
    list:[],
    wxids:[],
    modalOk:false
  },
  onLoad: function(options) {
    let obj = {};
    obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
        let statusBarHeight=res.statusBarHeight;
        this.setData({
          top: top,
          statusBarHeight:statusBarHeight
        })
      }
    });
    this.load();
  },
  setDropShow(){
    this.setData({
      dropShow:!this.data.dropShow
    })
  },
  setItem(e){
    let index=e.currentTarget.dataset.index;
    let index2=e.currentTarget.dataset.index2;
    let key='filter['+index+'].active';
    this.setData({
      [key]:index2
    })
  },
  closeOkPop(){
    this.setData({
      modalOk:false
    })
    this.getList();
  },
  toKehu(){
    wx.redirectTo({
      url:"/pages/workerCode/task"
    })
  },
  async getOrder(s){
    console.log(s);
    let item=s.detail;
    wx.util.showLoading('抢单中...',true);
    await wx.util.sleep(200);
    try{
      await wx.util.post("home/get_order",item.data);
      wx.hideLoading();
      this.setData({
        modalOk:true
      })
    }catch(e){
      wx.hideLoading();
      this.getList();
    }
    wx.requestSubscribeMessage({
      tmplIds: this.wxids,
      complete(res){}
    })
  },
  async load(){
    let res=await wx.util.post("home/index",{},false);
    this.setData({
      title:res.data.title,
      filter:res.data.filter,
      working:res.data.working,
      wxids:res.data.wxids
    })
    let ad=await wx.util.getLocation();
    this.setData({
      longitude:ad.longitude,
      latitude:ad.latitude
    })
    if(this.data.working==1){
      this.getList();
    }
    let ids=await wx.util.post("msg/get_wx_ids",{type:'worker'});
    this.setData({
      wxids:ids.data
    })
  },
  async getList(){
    this.setData({
      modalOk:false
    })
    if(this.data.working!=1){
      wx.util.toast('休息中无法接单');
      return;
    }
    let sub={};
    sub.longitude=this.data.longitude;
    sub.latitude=this.data.latitude;
    sub.km='';
    sub.price='';
    sub.cate='';
    if(this.data.filter.length>0){
      this.data.filter.forEach((v,i)=>{
        sub[v.key]=v.id[v.active];
      })
    }
    this.setData({
      loading:true
    })
    let res=await wx.util.post("home/orders",sub);
    this.setData({
      loading:false,
      list:res.data
    })
  },
  setWorking2(){
    this.setWorking({detail:{value:true}})
  },
  async setWorking(e){
    this.setData({
      working:e.detail.value===true?1:2
    })
    try{
      await wx.util.post("home/working",{w:this.data.working},false);
    }catch(e){
      this.setData({
        working:!this.data.working
      })
    }
    if(this.data.working==1){
      this.reset();
      this.getList();
    }else{
      this.setData({
        list:[]
      })
    }
    wx.requestSubscribeMessage({
      tmplIds: this.data.wxids,
      complete(res){}
    })
  },
  reset(){
    this.setData({
      hasFilter:0
    })
    let that=this;
    this.data.filter.forEach((v,i)=>{
      that.setData({
        ['filter['+i+'].active']:0
      })
    })
  },
  btnCloseDrop(){
    this.setData({
      dropShow:false,
      hasFilter:0
    })
    let that=this;
    this.data.filter.forEach((v,i)=>{
      if(v.active>0){
        that.setData({
          hasFilter:that.data.hasFilter++
        })
      }
    })
    this.getList();
  },
  initNavigation(e) {
    this.setData({
      opacity: 1,
      top: e.detail.top,
    })
  },

})